package edu.guet.cn.blogbasics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.guet.cn.blogbasics.common.ResultEnum;
import edu.guet.cn.blogbasics.common.ResultVO;
import edu.guet.cn.blogbasics.entity.Worksheet;
import edu.guet.cn.blogbasics.entity.WorksheetDone;
import edu.guet.cn.blogbasics.service.WorksheetDoneService;
import edu.guet.cn.blogbasics.service.WorksheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hejianding
 * @since 2021-04-29
 */
@RestController
@RequestMapping("/worksheet-done")
public class WorksheetDoneController {
    @Autowired
    WorksheetService worksheetService;
    @Autowired
    WorksheetDoneService worksheetDoneService;

    /**
     * 将完成任务插入完成表
     * @param worksheet
     * @return
     */
    @PostMapping("/submitToSuccess")
    public Map<String,Object> submitToSuccess(@RequestBody Worksheet worksheet){
        QueryWrapper<Worksheet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan", worksheet.getPlan())
                    .eq("user_name",worksheet.getUserName());
        Worksheet worksheet1= worksheetService.getOne(queryWrapper);
        WorksheetDone worksheetDone=new WorksheetDone();
            worksheetDone.setPlan(worksheet1.getPlan());
            worksheetDone.setPlanDetailed(worksheet1.getPlanDetailed());
            worksheetDone.setPlanStatus("以完成");
            worksheetDone.setUserName(worksheet1.getUserName());
            worksheetDone.setPlanTime(worksheet1.getPlanTime());
        System.out.println(worksheetDone);
        worksheetDoneService.save(worksheetDone);
        if(worksheetDone!=null){
            return ResultVO.result(ResultEnum.SUBMIT_WORKSHEET_SUCCESS, true);
        }else {
            return ResultVO.result(ResultEnum.SUBMIT_WORKSHEET_FAILED, false);
        }
    }

    /**
     * 按用户名查找以完成的任务信息
     * @param userName
     * @return
     */
    @GetMapping("/selectWorksheetDoneByUserName")
    public List<WorksheetDone> selectWorksheetDoneByUserName(String userName){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name", userName);
        List<WorksheetDone> worksheetDone = worksheetDoneService.list(wrapper);
        System.out.println(worksheetDone);
        return worksheetDone;
    }
}

